using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Elderlyhealth.DAL;
using System.IO;

public partial class Alerts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = false;
            BindThresholdSettings(); // 确保有数据绑定的代码在页面加载时执行
        }
    }

    protected void btnSaveThresholds_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvThresholdSettings.Rows)
        {
            int id = Convert.ToInt32(gvThresholdSettings.DataKeys[row.RowIndex].Value);
            string threshold = ((TextBox)row.FindControl("txtThreshold")).Text;
            string triggerCondition = ((DropDownList)row.FindControl("ddlTriggerCondition")).SelectedValue;

            // 获取选中的通知方式
            CheckBoxList cbNotificationMethod = (CheckBoxList)row.FindControl("cbNotificationMethod");
            List<string> selectedMethods = new List<string>();
            foreach (ListItem item in cbNotificationMethod.Items)
            {
                if (item.Selected)
                {
                    selectedMethods.Add(item.Value);
                }
            }
            string notificationMethods = string.Join(",", selectedMethods); // 合并成字符串

            using (var db = new DataClasses1DataContext())
            {
                    var indicator = db.Tsystem健康指标.FirstOrDefault(i => i.指标id == id);
                    if (indicator != null)
                    {
                        indicator.健康阈值 = threshold;
                        indicator.通知阈值条件 = triggerCondition;
                        indicator.通知方式 = notificationMethods;
                        db.SubmitChanges();
                    }
            }
        }

        // 显示自定义弹窗
        ScriptManager.RegisterStartupScript(this, GetType(), "showModal", "showModal();", true);
    }



    public void CheckAndGenerateAlerts(int old_id, string 指标名称, double 当前值)
    {
        using (var db = new DataClasses1DataContext())
        {
            var indicator = db.Tsystem健康指标.FirstOrDefault(i => i.指标名称 == 指标名称);
            if (indicator != null)
            {
                // 假设阈值范围是一个字符串 "60-100" 形式，需转换并判断
                var thresholds = indicator.健康阈值.Split('-');
                double min = Convert.ToDouble(thresholds[0]);
                double max = Convert.ToDouble(thresholds[1]);

                if (当前值 < min || 当前值 > max)
                {
                    var alert = new Talerts警报
                    {
                        old_id = old_id,
                        警报类型 = 指标名称,
                        描述 = $"{指标名称}超出安全范围: 当前值为 {当前值}",
                        时间 = DateTime.Now,
                        状态 = "待处理",
                        员工id = 获取处理员工ID(old_id)

                    };

                    db.Talerts警报.InsertOnSubmit(alert);
                    db.SubmitChanges();

                    // 发送通知
                    SendAlertNotification(alert);
                }
            }
        }
    }
    private int 获取处理员工ID(int old_id)
    {
        // 根据老人的 ID 或其他信息来确定具体的员工 ID
        // 此处假设你有一个员工分配表或规则
        // 这里简单返回一个示例 ID
        return 1002; // 根据具体情况返回相应的员工 ID
    }

    private void SendAlertNotification(Talerts警报 alert)
    {
        // 调用短信、邮件、系统推送的 API，发送给子女和社区工作人员
    }
    // 绑定健康指标到 GridView
    private void BindThresholdSettings()
    {
        using (var db = new DataClasses1DataContext())
        {
            var healthIndicators = db.Tsystem健康指标
                                     .Select(i => new
                                     {
                                         i.指标id,
                                         i.指标名称,
                                         i.健康阈值,
                                         通知阈值条件 = i.通知阈值条件 ?? "high", // 默认值为 "high"
                                         通知方式 = i.通知方式 ?? "sms" // 默认值为 "sms"
                                     }).ToList();

            gvThresholdSettings.DataSource = healthIndicators;
            gvThresholdSettings.DataBind();
        }
    }

    protected void typebtn_Click(object sender, EventArgs e)
    {
        if (Panel1.Visible)
        {
            Panel1.Visible = false;
            typetxt.Text = "";
        }
        else
        {
            Panel1.Visible = true;

            string filePath = @"D:\web系统与技术\Elderlyhealth\Elderlyhealths\Healthhtxt\健康数据阈值说明.txt";

            using (StreamReader reader = new StreamReader(filePath))
            {
                string line;
                typetxt.Text = "";
                while ((line = reader.ReadLine()) != null)
                {
                    typetxt.Text += line + "\n";
                }
            }
        }
    }
}



